/*
 * @lc app=leetcode.cn id=1143 lang=java
 *
 * [1143] 最长公共子序列
 */

// @lc code=start
class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        int len1= text1.length();
        int len2 = text2.length();
        int[][] dp = new int[len1+1][len2+1];
        for(int i = 1 ;i<len1 ;i++){
            for (int j = 1; j < len2; j++) {
                if(text1.charAt(i-1)==text2.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1]+1;
                }else{
                    dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j]);
                }
            }
        }
        return dp[len1][len2];
    }

    public int edit_distance(String s, String t) {
        int len1= s.length();
        int len2 = t.length();
        int[][] dp = new int[len1+1][len2+1];//开大一个
        for(int i = 1 ;i<=len2 ;i++){
            int[] temp = new int[len1+1];
            temp[i] = dp[i-1][i-1] + (t.charAt(i-1) == s.charAt(i-1)? 0:1);
            
            for (int j = i+1; j <= len1; j++) {
                if(s.charAt(i-1)==t.charAt(j-1)){
                    temp[j] = dp[i-1][j-1];
                }else{
                    temp[j] = Math.min(dp[i-1][j-1]+1, temp[j-1]);
                }
            }
            dp[i]=temp;
        }
        return dp[len1][len2]+len1-len2;
    }
}
// @lc code=end

